package com.bmadmin.service.impl;

import com.bmadmin.domain.NormalResult;
import com.bmadmin.domain.PluginZtreeDataPOJO;
import com.bmadmin.domain.SysDepartmentsPOJO;
import com.bmadmin.domain.SysUsersPOJO;
import com.bmadmin.mapper.SysDepartmentsPOJODao;
import com.bmadmin.mapper.SysUsersPOJODao;
import com.bmadmin.mapper.SysUsersRolesPOJODao;
import com.bmadmin.service.SysDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by SEELE on 2017/12/3.
 */
@Service
@Transactional
public class SysDepartmentServiceImpl implements SysDepartmentService {
    @Autowired
    SysDepartmentsPOJODao sysDepartmentsPOJODao;
    @Autowired
    SysUsersPOJODao sysUsersPOJODao;

    @Override
    public NormalResult insertDepartment(SysDepartmentsPOJO sysDepartmentsPOJO) {
        NormalResult result = new NormalResult();

        SysDepartmentsPOJO sysDepartmentsPOJOForQueryID = new SysDepartmentsPOJO();
        sysDepartmentsPOJOForQueryID.setD_id(sysDepartmentsPOJO.getD_id());
        List<SysDepartmentsPOJO> resultByQueryID = sysDepartmentsPOJODao.find(sysDepartmentsPOJOForQueryID);
        if (resultByQueryID.size() > 0) {
            result.setCode("9999");
            result.setMsg("该部门id已经存在");
            return result;
        }
        int resultForInsert = sysDepartmentsPOJODao.insertSelective(sysDepartmentsPOJO);
        if (resultForInsert == 1) {
            result.setCode("0000");
            result.setMsg("新增成功");
        } else {
            result.setCode("9999");
            result.setMsg("新增部门数据异常，请联系系统管理员");
        }

        return result;
    }

    @Override
    public NormalResult updateDepartment(SysDepartmentsPOJO sysDepartmentsPOJO) {
        NormalResult result = new NormalResult();

        int resultForUpdate = sysDepartmentsPOJODao.update(sysDepartmentsPOJO);
        if (resultForUpdate == 1) {
            result.setCode("0000");
            result.setMsg("修改成功");
        } else {
            result.setCode("9999");
            result.setMsg("修改部门数据异常，请联系系统管理员");
        }

        return result;
    }

    @Override
    public NormalResult delDepartment(SysDepartmentsPOJO sysDepartmentsPOJO) {
        NormalResult result = new NormalResult();
        SysDepartmentsPOJO sysDepartmentsPOJOForQueryChildren = new SysDepartmentsPOJO();
        sysDepartmentsPOJOForQueryChildren.setD_part_id(sysDepartmentsPOJO.getD_id());
        List<SysDepartmentsPOJO> resultByQueryChildren = sysDepartmentsPOJODao.find(sysDepartmentsPOJOForQueryChildren);
        if (resultByQueryChildren.size() > 0) {
            result.setCode("9999");
            result.setMsg("该部门存在下级部门,不得进行删除");
            return result;
        }
        SysUsersPOJO sysUsersPOJOForQueryDepartment = new SysUsersPOJO();
        sysUsersPOJOForQueryDepartment.setD_id(sysDepartmentsPOJO.getD_id());
        List<SysUsersPOJO> sysUsersPOJOSForQueryDepartment = sysUsersPOJODao.find(sysUsersPOJOForQueryDepartment);
        if (sysUsersPOJOSForQueryDepartment.size() > 0) {
            result.setCode("9999");
            result.setMsg("该部门下存在用户，不得进行删除");
            return result;
        }

        int resultForDelete = sysDepartmentsPOJODao.delete(sysDepartmentsPOJO);
        if (resultForDelete == 1) {
            result.setCode("0000");
            result.setMsg("删除成功");
        } else {
            result.setCode("9999");
            result.setMsg("删除部门数据异常，请联系系统管理员");
        }
        return result;

    }

    @Override
    public NormalResult getDepartmentByTree(SysDepartmentsPOJO sysDepartmentsPOJO) {
        NormalResult result = new NormalResult();
        List<PluginZtreeDataPOJO> pluginZtreeDataPOJOS = new ArrayList<PluginZtreeDataPOJO>();
        List<SysDepartmentsPOJO> sysDepartmentsPOJOS = sysDepartmentsPOJODao.find(sysDepartmentsPOJO);

        for (SysDepartmentsPOJO sysDepartmentsPOJOItem : sysDepartmentsPOJOS) {
            PluginZtreeDataPOJO forAddDepartment = new PluginZtreeDataPOJO();

            forAddDepartment.setId(sysDepartmentsPOJOItem.getD_id());
            forAddDepartment.setPart_id(sysDepartmentsPOJOItem.getD_part_id());
            forAddDepartment.setName(sysDepartmentsPOJOItem.getD_id() + " " + sysDepartmentsPOJOItem.getD_name());
            forAddDepartment.setOpen(true);
            forAddDepartment.setClick("getLineClick('" + sysDepartmentsPOJOItem.getD_id() + "')");

            pluginZtreeDataPOJOS.add(forAddDepartment);
        }

        for (PluginZtreeDataPOJO sysDepartmentsPOJOItemOne : pluginZtreeDataPOJOS) {
            List<PluginZtreeDataPOJO> forAddChildren = new ArrayList<PluginZtreeDataPOJO>();
            for (PluginZtreeDataPOJO sysDepartmentsPOJOItemTwo : pluginZtreeDataPOJOS) {
                if (sysDepartmentsPOJOItemOne.getId().equals(sysDepartmentsPOJOItemTwo.getPart_id())) {
                    forAddChildren.add(sysDepartmentsPOJOItemTwo);
                }
            }
            sysDepartmentsPOJOItemOne.setChildren(forAddChildren);
        }
        List<PluginZtreeDataPOJO> resultForLevelOne = new ArrayList<PluginZtreeDataPOJO>();
        for (PluginZtreeDataPOJO sysDepartmentsPOJOItem : pluginZtreeDataPOJOS) {
            if ("00".equals(sysDepartmentsPOJOItem.getPart_id())) {
                resultForLevelOne.add(sysDepartmentsPOJOItem);
            }
        }
        result.setCode("0000");
        result.setMsg("查询成功");
        result.setObject(resultForLevelOne);
        return result;

    }

    @Override
    public NormalResult getDepartmentById(SysDepartmentsPOJO sysDepartmentsPOJO) {
        NormalResult result = new NormalResult();
        List<SysDepartmentsPOJO> sysDepartmentsPOJOS = sysDepartmentsPOJODao.find(sysDepartmentsPOJO);
        if (sysDepartmentsPOJOS.size() > 0) {
            result.setCode("0000");
            result.setMsg("查询成功");
            result.setData(sysDepartmentsPOJOS.get(0));
        } else {
            result.setCode("9999");
            result.setMsg("查询数据为空");
        }

        return result;
    }
}
